using System.Collections.Generic;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ElevatorItem : BaseItem
	{
		public enum State
		{
			Open = 0,
			Close = 1
		}

		private enum Anim
		{
			Static = 0,
			Close = 1,
			Open = 2,
			CloseAndChangeScene = 3
		}

		private float openX = 3.4f;

		private float closeX = 1.33f;

		public int curSceneId;

		public int changeSceneId;

		private Transform leftDoor;

		private Transform rightDoor;

		private Transform btnsTrans;

		private List<Transform> btns = new List<Transform>();

		private Anim anim;

		private State state;

		private float animTimer;

		private AlertorItem alertorItem;

		private ElevatorButtonItem btnItem;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform trans = model.Find("Mask");
			trans.SetLocalZ(-1E-05f);
			leftDoor = model.Find("LeftDoor");
			rightDoor = model.Find("RightDoor");
			btnsTrans = model.Find("Btns");
			int childCount = btnsTrans.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = btnsTrans.GetChild(i);
				child.gameObject.layer = 15;
				btns.Add(child);
			}
			curSceneId = int.Parse(base.itemData.scriptParams);
			SetSelectBtn(curSceneId);
			state = (State)archiveData.state;
		}

		public override void ResetState()
		{
			base.ResetState();
			if (state == State.Close)
			{
				leftDoor.SetLocalX(0f - closeX);
				rightDoor.SetLocalX(closeX);
				SetPlatformColliderEnable(false);
				SetChildsColliderEnabled(false);
				SetAlertorEnabled(false);
				SetBtnState(ElevatorButtonItem.State.Close);
			}
		}

		public void SetBtnState(ElevatorButtonItem.State val)
		{
			if (btnItem == null)
			{
				BaseItem itemByScriptType = Singleton<Scene>.Instance.GetItemByScriptType(164);
				if (itemByScriptType != null)
				{
					btnItem = (ElevatorButtonItem)itemByScriptType;
				}
			}
			if (btnItem != null)
			{
				btnItem.SetState(val);
			}
		}

		public void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Close)
				{
					leftDoor.SetLocalX(0f - closeX);
					rightDoor.SetLocalX(closeX);
					SetBtnState(ElevatorButtonItem.State.Close);
				}
				else
				{
					leftDoor.SetLocalX(0f - openX);
					rightDoor.SetLocalX(openX);
					SetBtnState(ElevatorButtonItem.State.Open);
				}
			}
		}

		private void SetSelectBtn(int id)
		{
			for (int i = 0; i < btns.Count; i++)
			{
				if (btns[i].name.Equals(id.ToString()))
				{
					btns[i].Find("Normal").SetObjectActive(false);
					btns[i].Find("Selected").SetObjectActive(true);
				}
				else
				{
					btns[i].Find("Normal").SetObjectActive(true);
					btns[i].Find("Selected").SetObjectActive(false);
				}
			}
		}

		private void SetAlertorEnabled(bool val)
		{
			if (alertorItem == null)
			{
				BaseItem itemById = Singleton<Scene>.Instance.GetItemById("alertor2");
				if (itemById != null)
				{
					alertorItem = (AlertorItem)itemById;
				}
			}
			if (alertorItem != null)
			{
				alertorItem.SetModelEnabled(val);
			}
		}

		protected override void SetChildItemLayer(BaseItem item, int layer, int mapId)
		{
			base.SetChildItemLayer(item, layer, 101);
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			if (anim == Anim.Close || anim == Anim.CloseAndChangeScene)
			{
				animTimer += Time.deltaTime;
				leftDoor.SetLocalX(Mathf.Lerp(0f - openX, 0f - closeX, animTimer));
				rightDoor.SetLocalX(Mathf.Lerp(openX, closeX, animTimer));
				if (anim == Anim.Close)
				{
					if (animTimer >= 1f)
					{
						animTimer = 0f;
						anim = Anim.Static;
					}
				}
				else if (animTimer >= 1.6f)
				{
					animTimer = 0f;
					if (anim == Anim.CloseAndChangeScene && changeSceneId > 0)
					{
						GlobalEventSystem.Fire(new BaseEvent("game.change_scene_by_elevator", changeSceneId));
					}
					anim = Anim.Static;
				}
			}
			else if (anim == Anim.Open)
			{
				animTimer += Time.deltaTime;
				leftDoor.SetLocalX(Mathf.Lerp(0f - closeX, 0f - openX, animTimer));
				rightDoor.SetLocalX(Mathf.Lerp(closeX, openX, animTimer));
				if (animTimer >= 1.6f)
				{
					animTimer = 0f;
					anim = Anim.Static;
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && state == State.Close)
			{
				item.SetAllCollidersEnabled(false);
			}
			return flag;
		}

		public void OCDoor(bool needAudio = false)
		{
			if (anim == Anim.Static)
			{
				if (needAudio)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
				if (state == State.Open)
				{
					CloseDoor();
				}
				else if (state == State.Close)
				{
					OpenDoor();
				}
			}
		}

		public void CloseDoor()
		{
			if (anim == Anim.Static && state == State.Open)
			{
				anim = Anim.Close;
				Singleton<AudioManager>.Instance.PlaySound("sound_elevator_door");
				animTimer = 0f;
				state = State.Close;
				SetPlatformColliderEnable(false);
				SetChildsColliderEnabled(false);
				SetAlertorEnabled(false);
				SetBtnState(ElevatorButtonItem.State.Close);
			}
		}

		public void OpenDoor()
		{
			if (anim == Anim.Static && state == State.Close)
			{
				anim = Anim.Open;
				Singleton<AudioManager>.Instance.PlaySound("sound_elevator_door");
				animTimer = 0f;
				state = State.Open;
				SetPlatformColliderEnable(true);
				SetChildsColliderEnabled(true);
				SetAlertorEnabled(true);
				SetBtnState(ElevatorButtonItem.State.Open);
			}
		}

		public override void TouchUpOnSameItem(TouchEventData touchEventData)
		{
			base.TouchUpOnSameItem(touchEventData);
			if (anim != 0)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null && raycastHit2D.collider.transform.parent == btnsTrans && !raycastHit2D.collider.transform.name.Equals(curSceneId.ToString()))
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				changeSceneId = int.Parse(raycastHit2D.collider.transform.name);
				SetSelectBtn(changeSceneId);
				GlobalEventSystem.Fire(new BaseEvent("game.can_touch_ui", false));
				GlobalEventSystem.Fire(new BaseEvent("game.can_touch_scene", false));
				if (state == State.Close)
				{
					anim = Anim.CloseAndChangeScene;
					animTimer = 2f;
				}
				else
				{
					anim = Anim.CloseAndChangeScene;
					animTimer = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_elevator_door");
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
